<template>
	<view class="m-box">
		<u-toast ref="uToast"></u-toast>
		<view class="t-box">
			<view class="t-title">
				<view>金额</view>
				<view style="font-weight: bold;">￥{{amount}}/月</view>
			</view>
			<view class="t-title">
				<view>展示月数</view>
				<view style="display: flex;">
					<view>
						<u-number-box v-model="month" @change="valChange" :min="1"></u-number-box>
					</view>
					<view style="font-weight: bold;margin-left: 10px;line-height: 26px;">月</view>
				</view>
			</view>
			<view class="t-title">
				<view>小计</view>
				<view style="font-weight: bold;">￥{{total}} 元</view>
			</view>

		</view>
		<view>
			<view class="c-title">
				支付方式
			</view>
			<view class="f-pay">
				<view style="display: flex;line-height: 30px;">
					<view>
						<image style="height:30px; width: 30px;" src="/static/imgs/pay.png"></image>
						</image>
					</view>
					<view style="margin-left: 5px;">
						微信支付
					</view>
				</view>
				<view>
					<image style="height:20px; width: 20px;margin-top: 3px;" src="/static/imgs/gou.png"></image>
				</view>
			</view>
		</view>
		<view style="margin-top: 50px;width: 100%;">
			<u-button @click="submit" :custom-style="customStyle" shape="circle" type="success">立即支付</u-button>
			<view @click="toBack" style="margin-top: 12px;color: #aaa;text-align: center;font-size: 14px;">返回上一步
			</view>
		</view>

	</view>
</template>

<script>
	import {
		orderApis,
		payApis,
	} from '@/apis/index'

	export default {
		onLoad(obj) {
			this.orderInfo = uni.getStorageSync('orderInfo');
		},
		data() {
			return {
				month: '1',
				amount: 199,
				total: 199,
				customStyle: {
					backgroundColor: '#009688'
				},
				orderInfo: {},
				successInfo: {
					type: 'success',
					title: "发布成功,等待审批!",
					duration: 2000
				}
			}
		},
		methods: {
			getOrderModel() {
				var _this = this
				orderApis.getOrderModel().then((res) => {

					_this.amount = res.data.data.amount
					_this.total = res.data.data.amount
				})
			},

			showToast(params) {
				this.$refs.uToast.show(params)
			},

			handleOrder() {
				var _this = this
				orderApis.addOrder(_this.orderInfo).then((res) => {
					console.log(res)
					//保存消费记录
					let serviceId = res.data.data.id

					let para = {
						serviceId: serviceId,
						amount: _this.amount,
						number: _this.month,
						total: _this.total
					}
					orderApis.addModelRecord(para).then((res) => {
						//回退到列表页
						_this.showToast(_this.successInfo)

						setTimeout(function() {
							uni.$emit('orderFresh', true);

							uni.switchTab({
								url: '/pages/order/order/order'
							})
						}, 3000)
					})
				})
			},

			submit() {
				var _this = this

				_this.orderInfo.amount = _this.amount
				_this.orderInfo.number = _this.month
				_this.orderInfo.month = _this.month
				_this.orderInfo.total = _this.total
				
				let para = {
					title: '本地一手发布套餐',
					amount: _this.amount,
					localService: _this.orderInfo
				}

				let jsonData = {
					jsonStr: JSON.stringify(para)
				}

				console.log("jsonData:", jsonData)
		
				payApis.getOrder(jsonData).then((res) => {
					let data = res.data;
					if (data.code == 0) {
						wx.requestPayment({
							timeStamp: data.data.timeStamp,
							nonceStr: data.data.nonceStr,
							package: data.data.packageValue,
							signType: data.data.signType,
							paySign: data.data.paySign,
							success(res) {
								wx.showToast({
									title: '支付成功',
									icon: 'success'
								});

								//_this.handleOrder()

								//回退到列表页
								// _this.showToast(_this.successInfo)

								setTimeout(function() {
									uni.$emit('orderFresh', true);

									uni.switchTab({
										url: '/pages/order/order/order'
									})
								}, 3000)
							},
							fail(res) {
								console.error('支付失败', res);
								wx.showModal({
									title: '支付失败',
									content: '支付过程中出现问题',
									showCancel: false
								});
							}
						})
					}
				})
			},
			valChange(e) {
				console.log(e)
				this.total = e.value * this.amount
			},
			toBack() {
				uni.navigateBack()
			}
		},
		mounted() {
			this.getOrderModel()
		}
	}
</script>

<style lang="scss" scoped>
	.m-box {
		padding: 20px;
		background-color: white;
		height: 100vh;
		font-size: 15px;
	}

	.t-box {}

	.t-title {
		display: flex;
		justify-content: space-between;
		padding-top: 15px;
		padding-bottom: 15px;
		border-bottom: 1px solid #ededed;
	}

	.c-title {
		padding-top: 15px;
		padding-bottom: 15px;
		margin-top: 20px;
	}

	.f-pay {
		margin-top: 20px;
		display: flex;
		justify-content: space-between;
		font-size: 14px;
	}
</style>